package com.ruoyi.system.service.impl;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.config.WxCpConfiguration;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.IWxService;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.WxCpDepart;
import me.chanjar.weixin.cp.bean.WxCpUser;
import me.chanjar.weixin.cp.bean.message.WxCpMessage;
import me.chanjar.weixin.cp.bean.messagebuilder.TemplateCardBuilder;
import me.chanjar.weixin.cp.bean.messagebuilder.TextBuilder;
import me.chanjar.weixin.cp.bean.messagebuilder.TextCardBuilder;
import org.springframework.stereotype.Service;

import java.util.List;

@RequiredArgsConstructor
@Service
public class WxServiceImpl implements IWxService {


    private final static Long PARENT_DEPT_ID = 1L;
    private final ISysDeptService sysDeptService;

    private final ISysUserService sysUserService;

    @Override
    public boolean sysDept() throws WxErrorException {
        WxCpService cpService = WxCpConfiguration.getCpService();
        List<WxCpDepart> list = cpService.getDepartmentService().list(PARENT_DEPT_ID);
        for (WxCpDepart wxCpDepart : list) {
            SysDept sysDept = new SysDept();
            sysDept.setDeptId(wxCpDepart.getId());
            sysDept.setParentId(wxCpDepart.getParentId());
            sysDept.setDeptName(wxCpDepart.getName());
            sysDeptService.saveOrupdateDept(sysDept);
        }
        return false;
    }

    @Override
    public boolean sysUser() throws WxErrorException {
        WxCpService cpService = WxCpConfiguration.getCpService();
        cpService.getAccessToken();
        List<WxCpUser> wxCpUsers = cpService.getUserService().listByDepartment(PARENT_DEPT_ID, true, 0);
        for (WxCpUser wxCpUser : wxCpUsers) {
            SysUser user = new SysUser();
            if(ObjectUtil.isEmpty(wxCpUser.getDepartIds())){
                continue;
            };
            user.setDeptId(wxCpUser.getDepartIds()[0]);
            user.setUserName(wxCpUser.getUserId());
            user.setPhonenumber(wxCpUser.getMobile());
            user.setNickName(wxCpUser.getName());
            user.setPassword("$2a$10$Vitdj9DV2OUxJXVcN6XEyuSJ7idrkhebsFrovXmtAf4BqTfUzwj3y");
            user.setRoleIds(new Long[]{1646160914518196226L});
            sysUserService.saveOrupdateUser(user);
        }
        return false;
    }

    @Override
    public void sendMessage2Copy(List<SysUser> toUser,String url,String description) {
        sendMessage2Copy(this.parseUsers2String(toUser),url,description);
    }

    @Override
    public void sendMessage2Copy(String toUser, String url, String description) {
        WxCpService cpService = WxCpConfiguration.getCpService();
        TextCardBuilder textcard = WxCpMessage.TEXTCARD();
        textcard.toUser(toUser);
        textcard.title("任务状态变更通知");
        textcard.description(description);
        textcard.url(url);
        textcard.btnTxt("详情查看");
        WxCpMessage wxCpMessage = textcard.build();
        try {
            cpService.getMessageService().send(wxCpMessage);
        } catch (WxErrorException e) {
            throw new RuntimeException(e);
        }
    }
    @Override
    public void sendMessageByText2Copy(String toUser, String url,String title, String description) {
        String task = "流程变化:<a href=\""+url+"\">"+title+"</a>\n";
        WxCpService cpService = WxCpConfiguration.getCpService();
        TextBuilder text = WxCpMessage.TEXT();
        text.content(task + description);
        text.toUser(toUser);
        WxCpMessage wxCpMessage = text.build();
        try {
            cpService.getMessageService().send(wxCpMessage);
        } catch (WxErrorException e) {
            throw new RuntimeException(e);
        }
    }

    private String parseUsers2String(List<SysUser> userList){
        String toUser = "";

        for (SysUser sysUser : userList) {
            if(StringUtils.isNotBlank(toUser)){
                toUser += "|";
            }
            toUser += sysUser.getUserName();
        }
        return toUser;
    }

    @Override
    public void sendMessage2Head(List<SysUser> toUser,String url,String description) {
        TemplateCardBuilder templatecard = WxCpMessage.TEMPLATECARD();
        templatecard.cardType("text_notice");
    }

}
